#include "MainWindow.h"
#include <QResizeEvent>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QList>
#include <QStringList>
#include <QFileDialog>
#include <qdebug.h>
#

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
    resize(800,600);
    setWindowTitle("Painter");
    setFixedSize(800,600);
    graphicsView = new QGraphicsView();
    graphicsView->resize(500, 450);
    squareB = new QPushButton("Квадрат");
    triangleB = new QPushButton("Треугольни");
    circleB = new QPushButton("Круг");
    save = new QPushButton("Сохарнить");
    open = new QPushButton("Открыть");
    QHBoxLayout *hlayout1 = new QHBoxLayout;
    hlayout1->addWidget(squareB);
    hlayout1->addWidget(triangleB);
    hlayout1->addWidget(circleB);
    QHBoxLayout *hlayout2 = new QHBoxLayout;
    hlayout2->addWidget(open);
    hlayout2->addWidget(save);
    QVBoxLayout *vlayout = new QVBoxLayout;
    vlayout->addLayout(hlayout2);
    vlayout->addWidget(graphicsView);
    vlayout->addLayout(hlayout1);
    setLayout(vlayout);
    scene = new QGraphicsScene();
    scene->setSceneRect(50,80,700,450);
    graphicsView->setScene(scene);
    connect(squareB, SIGNAL(clicked(bool)), SLOT(rectanglePaint()));
    connect(triangleB, SIGNAL(clicked(bool)), SLOT(trianglePaint()));
    connect(circleB, SIGNAL(clicked(bool)), SLOT(circlePaint()));
    connect(save, SIGNAL(clicked(bool)), SLOT(saveFile()));
    connect(open, SIGNAL(clicked(bool)), SLOT(openFile()));
}

MainWindow::~MainWindow()
{
    for (int i =0;i<list.size();i++)
    {
        delete list[i];
    }

    delete graphicsView;
    delete squareB;
    delete triangleB;
    delete circleB;
    delete scene;
    delete save;
    delete open;

}

void MainWindow::openFile()
{

    QString strFile = QFileDialog::getOpenFileName(this);
    for (int i=0;i<conteiner.size();i++)
    {
        conteiner.removeFig(0);
    }
    conteiner.readFromFile(strFile);
    scene->clear();
    for(int i=0; i<conteiner.size(); i++)
    {
        if(conteiner[i]->print_name()== "Square")
        {
            QList<QString> x  =conteiner[i]->print_inf();
            QString f1= x.first();
            x.pop_front();
            QString f2= x.first();
            x.pop_front();
            QString f3= x.first();
            QString f4=x.last();
            float x1=f1.toFloat();
            float x2=f2.toFloat();
            float x3=f3.toFloat();
            float x4=f4.toFloat();
            square *item = new square("Square",x1,x2,x3,x4);
            item->setPos(x2,x3);
            item->setFlag(QGraphicsItem::ItemIsMovable,1);
            item->setFlag(QGraphicsItem::ItemIsFocusable,1);
            item->setFlag(QGraphicsItem::ItemIsSelectable,1);
            scene->addItem(item);
            item->setRotation(item->ang);
            list.push_back(item);
        }

        if(conteiner[i]->print_name() == "Triangle")
        {
            QList<QString> x  =conteiner[i]->print_inf();
            QString f1= x.first();
            x.pop_front();
            QString f2= x.first();
            x.pop_front();
            QString f3= x.first();
            QString f4=x.last();
            float x1=f1.toFloat();
            float x2=f2.toFloat();
            float x3=f3.toFloat();
            float x4=f4.toFloat();
            triangle *item = new triangle("Triangle",x1,x2,x3,x4);
            item->setPos(x2,x3);
            item->setFlag(QGraphicsItem::ItemIsMovable,1);
            item->setFlag(QGraphicsItem::ItemIsFocusable,1);
            item->setFlag(QGraphicsItem::ItemIsSelectable,1);
            scene->addItem(item);
            item->setRotation(item->ang);
            list.push_back(item);
        }

        if(conteiner[i]->print_name() == "Circle")
        {
            QList<QString> x  = conteiner[i]->print_inf();
            QString f1= x.first();
            x.pop_front();
            QString f2= x.first();
            x.pop_front();
            QString f3= x.first();
            QString f4=x.last();
            float x1=f1.toFloat();
            float x2=f2.toFloat();
            float x3=f3.toFloat();
            float x4=f4.toFloat();
            circle *item = new circle("Circle",x1,x2,x3,x4);
            item->setPos(x2,x3);
            item->setFlag(QGraphicsItem::ItemIsMovable,1);
            item->setFlag(QGraphicsItem::ItemIsFocusable,1);
            item->setFlag(QGraphicsItem::ItemIsSelectable,1);
            scene->addItem(item);
            item->setRotation(item->ang);
            list.push_back(item);
        }
    }
    for (int i=0;i<conteiner.size();i++)
    {
        conteiner.removeFig(0);
    }
}

void MainWindow::saveFile()
{
    QString strFile = QFileDialog::getSaveFileName(this);
    for(int i=0;i<conteiner.size();i++)
    {
        conteiner.removeFig(0);
    }

    for (int i=0;i<list.size();i++)
    {
        if(list[i]->type=="Circle")
        {
            Circle *c= new Circle(list[i]->size,list[i]->coordX,list[i]->coordY,list[i]->ang);
            conteiner.pushFig(c);
        }
        if(list[i]->type=="Triangle")
        {
            Triangle *t= new Triangle(list[i]->size,list[i]->coordX,list[i]->coordY,list[i]->ang);
            conteiner.pushFig(t);
        }
        if(list[i]->type=="Square")
        {
            Square *s= new Square(list[i]->size,list[i]->coordX,list[i]->coordY,list[i]->ang);
            conteiner.pushFig(s);
        }

    }
    QFile fileOut(strFile);
    if(conteiner.size() == 0)
    {
        fileOut.resize(0);
        return;
    }
    fileOut.resize(0);
    conteiner.zapisb(strFile);
}


void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    if((triangleB->isEnabled() && !squareB->isEnabled() && !circleB->isEnabled())||
            (!triangleB->isEnabled() && !squareB->isEnabled() && circleB->isEnabled())||
                (!triangleB->isEnabled() && squareB->isEnabled() && !circleB->isEnabled()))
    {
      x = event->x();
      y = event->y();
      paintShape();
    }
}

void MainWindow::paintShape()
{
        if (squareB->isEnabled())
        {
            square *item = new square("Square");  
            item->setPos(x,y);
            item->coordX = x;
            item->coordY = y;
            item->size = 50;
            item->ang = 0;
            circleB->setEnabled(true);
            triangleB->setEnabled(true);
            item->setFlag(QGraphicsItem::ItemIsMovable, true);
            item->setFlag(QGraphicsItem::ItemIsSelectable, true);
            item->setFlag(QGraphicsItem::ItemIsFocusable,true);
            scene->addItem(item);
            list.push_back(item);
        }
        else if (triangleB->isEnabled())
        {
            triangle *item = new triangle("Triangle");
            item->setPos(x,y);
            item->coordX = x;
            item->coordY = y;
            item->size = 50;
            item->ang = 0;
            circleB->setEnabled(true);
            squareB->setEnabled(true);
            item->setFlag(QGraphicsItem::ItemIsMovable, true);
            item->setFlag(QGraphicsItem::ItemIsSelectable, true);
            item->setFlag(QGraphicsItem::ItemIsFocusable,true);
            scene->addItem(item);
            list.push_back(item);
        }
        else if (circleB->isEnabled())
        {
            circle *item = new circle("Circle");
            item->setPos(x,y);
            item->coordX = x;
            item->coordY = y;
            item->size = 50;
            item->ang= 0;
            triangleB->setEnabled(true);
            squareB->setEnabled(true);
            item->setFlag(QGraphicsItem::ItemIsMovable, true);
            item->setFlag(QGraphicsItem::ItemIsSelectable, true);
            item->setFlag(QGraphicsItem::ItemIsFocusable,true);
            scene->addItem(item);
            list.push_back(item);
        }
}

void MainWindow::circlePaint()
{

    squareB->setEnabled(0);
    triangleB->setEnabled(0);
}

void MainWindow::trianglePaint()
{
    squareB->setEnabled(0);
    circleB->setEnabled(0);
}

void MainWindow::rectanglePaint()
{
    circleB->setEnabled(0);
    triangleB->setEnabled(0);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    QList<QGraphicsItem*> selectedItems = scene->selectedItems();
    QList <QGraphicsItem* > lst  =scene->items();
    qDebug()<<selectedItems.size();
    if (selectedItems.size()!=0)
    {
        if(event->key() == Qt::Key_A)
        {
            for (int i=0; i<selectedItems.size(); i++)
            {
                selectedItems[i]->setRotation((selectedItems[i]->rotation())-10);
            }
        }
        if(event->key() == Qt::Key_D)
        {
            for (int i=0; i<selectedItems.size(); i++)
            {
                selectedItems[i]->setRotation((selectedItems[i]->rotation())+10);
            }
        }
    }
}

void MainWindow ::mousePressEvent(QMouseEvent *event)
{
    QList <QGraphicsItem* > lst  =scene->items();
    for(int i=0;i<lst.size();i++)
    {
        shapes* ans=(shapes*)lst[i];
        ans->machinators();
    }
}
